﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KingSite.Library.Mail;
using System.Net.Mail;
using System.Net;
using KingSite.Library.IO;

namespace kingSite.Library.Mail.DotNetMailWrapper {
    public class DotNetMailWrapper : IMail {
        IMailSetting mailSetting = new MailSetting();

        public DotNetMailWrapper() {
            this.Server = mailSetting.Server;
            this.Username = mailSetting.Username;
            this.Password = mailSetting.Password;
            this.SenderAddress = mailSetting.SenderAddress;
            this.SenderName = mailSetting.SenderName;
            this.Port = mailSetting.Port;
        }

        public void Send(string toAddress, string subject, string body, bool isBodyHtml) {
            this.Send(toAddress, subject, body, isBodyHtml, null, false);
        }

        public void Send(string toAddress, string subject, string body, bool isBodyHtml, params string[] attachments) {
            this.Send(toAddress, subject, body, isBodyHtml, attachments, false);

        }

        public void Send(string toAddress, string subject, string body, bool isBodyHtml, string[] attachments, bool async) {
            Action<string> action = null;
            SmtpClient client = new SmtpClient(this.Server);
            if (!string.IsNullOrEmpty(this.Username)) {
                NetworkCredential credential = new NetworkCredential(this.Username, this.Password);
                client.Credentials = credential;
            }
            if (this.Port != 0) {
                client.Port = this.Port;
            }
            client.EnableSsl = this.EnabledSSL;


            //MailAddress to = new MailAddress(toAddress);
            MailAddress from = new MailAddress(this.SenderAddress, this.SenderName);
            using (MailMessage message = new MailMessage()) {
                foreach (var item in toAddress.Split(';'))
                    message.To.Add(item);
                message.From = from;
                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = isBodyHtml;
                if ((attachments != null) && (attachments.Length > 0)) {
                    if (action == null)
                        action = file => message.Attachments.Add(new Attachment(PathUtility.GetPhysicalPath(file)));
                    Array.ForEach<string>(attachments, action);
                }
                if (async)
                    client.SendAsync(message, null);
                else
                    client.Send(message);
            }
        }

        public void SendAsync(string toAddress, string subject, string body, bool isBodyHtml) {
            this.Send(toAddress, subject, body, isBodyHtml, null, true);
        }

        public void SendAsync(string toAddress, string subject, string body, bool isBodyHtml, params string[] attachments) {
            this.Send(toAddress, subject, body, isBodyHtml, attachments, true);
        }

        public bool EnabledSSL {
            get;
            set;
        }

        public string Password {
            get;
            set;
        }

        public int Port {
            get;
            set;
        }

        public string SenderAddress {
            get;
            set;
        }

        public string SenderName {
            get;
            set;
        }

        public string Server {
            get;
            set;
        }

        public string Username {
            get;
            set;
        }
    }
}
